<?php
require_once 'Zend/Uri.php';

class Kornak_Http_Crawler_UrlNormalizer
{
    static public function normalize($url)
    {
        $uri = Zend_Uri::Factory($url);

        $uri = self::removeFragment($uri);
        $uri = self::removeDefaultPort($uri);

        return $uri->getUri();
    }

    static public function removeDefaultPort($uri)
    {
        $port = $uri->getPort();

        if (!empty($port)) {
            switch ($uri->getScheme()) {
                case 'http':
                    $defaultPort = 80;
                    break;
                case 'https':
                    $defaultPort = 443;
                    break;
            }
            if ($defaultPort == $port) {
                $uri->setPort('');
            }
        }

        return $uri;
    }

    static public function removeFragment($uri)
    {
        $uri->setFragment('');
        return $uri;
    }
}